package com.leoman.spinningmachine.login;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.leoman.helper.util.LogUtil;
import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.PhoneMac;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity {

    @BindView(R.id.et_gh)
    EditText et_gh;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.tv_yzcode)
    TextView tv_yzcode;
    @BindView(R.id.et_yzcode)
    EditText et_yzcode;

    @BindView(R.id.iv_pwd)
    ImageView ivPwd;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.iv_pwd2)
    ImageView ivPwd2;
    @BindView(R.id.et_pwd2)
    EditText etPwd2;

    @BindView(R.id.tv_commit)
    TextView tvCommit;

    private boolean isShowPassWord, isShowPassWord2;
    private String phoneStr;
    private String ghStr;
    private String yzCode;
    private String password;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_register;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setBarColor(false, R.color.trans);
        setFullscreen(true);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.iv_pwd, R.id.iv_pwd2, R.id.tv_commit, R.id.iv_back, R.id.tv_yzcode})
    void click(View view) {
        switch (view.getId()) {
            case R.id.iv_pwd:
                password = etPwd.getText().toString().trim();
                isShowPassWord = !isShowPassWord;
                if (isShowPassWord) {
                    ivPwd.setImageResource(R.drawable.ic_eye_open);
                    etPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    ivPwd.setImageResource(R.drawable.ic_eye_close);
                    etPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                if (!TextUtils.isEmpty(password)) {
                    etPwd.setSelection(etPwd.getText().length());
                }
                break;
            case R.id.iv_pwd2:
                String password2 = etPwd2.getText().toString().trim();
                isShowPassWord2 = !isShowPassWord2;
                if (isShowPassWord2) {
                    ivPwd2.setImageResource(R.drawable.ic_eye_open);
                    etPwd2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    ivPwd2.setImageResource(R.drawable.ic_eye_close);
                    etPwd2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                if (!TextUtils.isEmpty(password2)) {
                    etPwd2.setSelection(etPwd.getText().length());
                }
                break;
            case R.id.tv_commit:
                commit();
                break;
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_yzcode:
                sendCode();
                break;
        }
    }

    /**
     * 发送验证码
     */
    private void sendCode() {
        phoneStr = et_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phoneStr)) {
            ToastUtil.showToast(this, "请输入手机号", Toast.LENGTH_LONG);
            return;
        }
        sentVetify(tv_yzcode);
        DialogUtils.getInstance().show(this);
        WebServiceApi.getInstance().sendcodeRequest(phoneStr, "register", this, 1);
    }

    private void commit() {
        ghStr = et_gh.getText().toString().trim();
        phoneStr = et_phone.getText().toString().trim();
        yzCode = et_yzcode.getText().toString().trim();
        password = etPwd.getText().toString().trim();
        String password2 = etPwd2.getText().toString().trim();
        if (TextUtils.isEmpty(ghStr)) {
            DialogUtils.getInstance().showResultDialog(this, "请输入您的工号");
        } else if (TextUtils.isEmpty(phoneStr)) {
            DialogUtils.getInstance().showResultDialog(this, "请输入您的手机号");
        } else if (!phoneStr.matches(Constant.ISPHONENUM)) {
            DialogUtils.getInstance().showResultDialog(this, "手机号格式不正确");
        } else if (TextUtils.isEmpty(yzCode)) {
            DialogUtils.getInstance().showResultDialog(this, "请输入短信验证码");
        } else if (TextUtils.isEmpty(password)) {
            DialogUtils.getInstance().showResultDialog(this, "请输入您的密码");
        } else if (TextUtils.isEmpty(password2)) {
            DialogUtils.getInstance().showResultDialog(this, "请再次输入您的密码");
        } else if (!password2.equals(password)) {
            DialogUtils.getInstance().showResultDialog(this, "两次输入密码不一致");
        } else {
            LogUtil.json("mac:" + PhoneMac.getLocalMacAddressFromIp());
            String phoneMac = PhoneMac.getLocalMacAddressFromIp();
            DialogUtils.getInstance().show(this);
            WebServiceApi.getInstance().register(ghStr, phoneStr, yzCode, password, password2, phoneMac, this, 2);
        }
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        DialogUtils.getInstance().cancel();
        if (1 == tag) {
            et_yzcode.setText("" + apiResponse.data.code);
            LogUtil.json("验证码：" + apiResponse.data.code);
        } else if (2 == tag) {
            gotoOtherActivity(LoginActivity.class);
            finish();
            LogUtil.json("token:" + apiResponse.data.token);
        }
    }

    @Override
    public void OnErrorData(String code, Integer tag) {
        DialogUtils.getInstance().cancel();
        DialogUtils.getInstance().showResultDialog(this, code);
    }

    @Override
    public void OnFailureData(String error, Integer tag) {
        super.OnFailureData(error, tag);
        DialogUtils.getInstance().cancel();
    }

}
